package com.zhong.beautifulimageapp;

import android.Manifest;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.widget.CardView;
import android.util.Log;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.rengwuxian.materialedittext.MaterialEditText;
import com.zhong.beautifulimageapp.model.CommonVariable;
import com.zhong.beautifulimageapp.model.UserAfterLoginInfo;
import com.zhong.beautifulimageapp.network.GetContentFromNetwork;
import com.zhong.beautifulimageapp.utils.RequestPermissionInterface;
import com.zhong.zxing.codescan.MipcaActivityCapture;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class LoginActivity extends BaseActivity implements RequestPermissionInterface {

    @BindView(R.id.login_back)
    ImageView mLoginBack;
    @BindView(R.id.login_title)
    TextView mLoginTitle;
    @BindView(R.id.login_header)
    LinearLayout mLoginHeader;
    @BindView(R.id.login_accessToken)
    MaterialEditText mLoginAccessToken;
    @BindView(R.id.login_login_button)
    Button mLoginLoginButton;
    @BindView(R.id.login_from_swap)
    TextView mLoginFromSwap;
    @BindView(R.id.login_from_github)
    TextView mLoginFromGithub;
    @BindView(R.id.login_card)
    CardView mLoginCard;
    @BindView(R.id.activity_login)
    RelativeLayout mActivityLogin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        setPermissionInterface(this);
    }

    /**
     * 点击扫码登录后的逻辑
     */
    @OnClick(R.id.login_from_swap)
    public void swap(){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
            dynamicApplication(new String[]{Manifest.permission.CAMERA});
        }else {
            goToSwap();
        }
    }

    /**
     * 启动扫一扫功能
     */
    private void goToSwap() {
        Intent intent = new Intent(this, MipcaActivityCapture.class);
        startActivityForResult(intent, REQUEST_FOR_CAMERA_CODE);
    }

    /**
     * 返回逻辑
     */
    @OnClick(R.id.login_back)
    public void goBack(){
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        finish();
    }

    /**
     * 授权成功
     */
    @Override
    public void success() {
        goToSwap();
    }

    /**
     * 存储AccessToken成功后的回调
     * 进入登录逻辑
     */
    @Override
    public void successForCamera() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(CommonVariable.baseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
        GetContentFromNetwork network = retrofit.create(GetContentFromNetwork.class);
        network.userLogin(getTheAccessTokenFromStorage())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<UserAfterLoginInfo>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull UserAfterLoginInfo userAfterLoginInfo) {
                        if (userAfterLoginInfo.isSuccess()) {
                            Toast.makeText(LoginActivity.this, "用户登录成功", Toast.LENGTH_SHORT).show();
                            setTheUserAfterLoginInfo(userAfterLoginInfo.getLoginName(),
                                    userAfterLoginInfo.getId(),
                                    userAfterLoginInfo.getUserIcon());
                            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                            startActivity(intent);
                            finish();
                        }else {
                            Toast.makeText(LoginActivity.this, "用户登录失败", Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        Toast.makeText(LoginActivity.this, "用户登录失败", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
